package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlog;
import pers.myz.entity.SnsFriendEvent;
import pers.myz.entity.SnsProject;
import pers.myz.service.SnsFriendEventService;
import pers.myz.service.SnsProjectService;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-project")
public class SnsProjectController {

    @Autowired
    private SnsProjectService snsProjectService;


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsProject snsProject){return snsProjectService.save(snsProject)?Result.suc():Result.fail();}

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody SnsProject snsProject){return snsProjectService.updateById(snsProject)?Result.suc():Result.fail();}

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){return snsProjectService.removeById(id)?Result.suc():Result.fail();}

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String title = (String)param.get("title");
        String creatorId = (String)param.get("creatorId");


        IPage<SnsProject> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<SnsProject> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(title) && !"null".equals(title)){
            lambdaQueryWrapper.like(SnsProject::getTitle,title);
        }
        if(StringUtils.isNotBlank(creatorId) && !"null".equals(creatorId)){
            lambdaQueryWrapper.eq(SnsProject::getCreatorId,creatorId);
        }

        IPage result = snsProjectService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }


}
